home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / OUI / new.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.6 KB  |  146 lines

  1. // $Id: new.cc 1.3 1997/09/17 08:16:26 dlorre Exp dlorre $
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4.  
  5. #include <dos/dos.h>
  6. #include <dos/dosextens.h>
  7.  
  8. #include <intuition/intuition.h>
  9.  
  10. #include <new.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <proto/intuition.h>
  17. #include <clib/alib_protos.h>
  18. #include <mydebug.h>
  19. #include <compiler.h>
  20.  
  21. static ULONG OxygeneSize = 20000 ;
  22. APTR chipOxygene ;
  23.  
  24. static SignalSemaphore* MemorySemaphore ;
  25.  
  26. static Process *myproc ;
  27.  
  28. APTR anyPool=NULL, chipPool=NULL ;
  29.  
  30. static EasyStruct memFail = {
  31.     sizeof (struct EasyStruct),
  32.     0,
  33.     (UBYTE *)"Memory Warning",
  34.     (UBYTE *)"The system is running into a low memory situation.\n"
  35.     "I have been unable to allocate %lu bytes.\n\n"
  36.     "%lu bytes of chip memory have been given back to the system.\n"
  37.     "I can wait until you free some more and I can reallocate it.\n"
  38.     "Or, if you select Cancel I will not take back those bytes.\n"
  39.     "and %s allocations might be rejected.",
  40.     (UBYTE *)"Wait|Cancel",
  41. };
  42.  
  43.  
  44. LONG SAVEDS outmem(size_t s)
  45. {
  46. BOOL reply ;
  47.  
  48.     GETA4 ;
  49.  
  50.     LibFreePooled(chipPool, chipOxygene, OxygeneSize) ;
  51.     chipOxygene = NULL ;
  52.     reply = (BOOL)EasyRequest((Window *)myproc->pr_WindowPtr, &memFail,
  53.         NULL, s, OxygeneSize, myproc->pr_Task.tc_Node.ln_Name) ;
  54.     if (reply == 1) {
  55.         while(!(chipOxygene = LibAllocPooled(chipPool, OxygeneSize))) {
  56.             Delay(200) ;
  57.         }
  58.  
  59.     }
  60.     return reply ;
  61. }
  62.  
  63. void *operator new(size_t s)
  64. {
  65. unsigned long *p ;
  66.  
  67.     ObtainSemaphore(MemorySemaphore) ;
  68.     s += 4 ;
  69.     while ((p = (unsigned long *)LibAllocPooled(anyPool, s)) == NULL) {
  70.         if (chipOxygene) {
  71.             if (!outmem(s)) {
  72.                 p = (unsigned long *)LibAllocPooled(anyPool, s) ;
  73.             }
  74.         }
  75.         else
  76.             return NULL ;
  77.     }
  78.     *p++ = s ;
  79.     ReleaseSemaphore(MemorySemaphore) ;
  80.     return (void *)p ;
  81. }
  82.  
  83. void operator delete(void *p)
  84. {
  85. unsigned long *l = (unsigned long *)(p) ;
  86.  
  87.     ObtainSemaphore(MemorySemaphore) ;
  88.     l-- ;
  89.     LibFreePooled(anyPool, l, *l) ;
  90.     ReleaseSemaphore(MemorySemaphore) ;
  91. }
  92.  
  93. extern "C" {
  94. int _STI_50_initpool()
  95. {
  96.    myproc = (Process *)FindTask(0);
  97.  
  98.     MemorySemaphore = (SignalSemaphore *)AllocMem(sizeof(SignalSemaphore), MEMF_CLEAR|MEMF_PUBLIC) ;
  99.     if (MemorySemaphore) {
  100.         InitSemaphore(MemorySemaphore);
  101.  
  102.         anyPool = LibCreatePool(MEMF_CLEAR|MEMF_PUBLIC, 10000, 5000) ;
  103.         if (anyPool) {
  104.             chipPool = LibCreatePool(MEMF_CLEAR|MEMF_CHIP, 20000, 10000) ;
  105.             if (!chipPool) {
  106.                 LibDeletePool(anyPool) ;
  107.                 anyPool = NULL ;
  108.                 FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  109.                 MemorySemaphore = NULL ;
  110.             }
  111.             chipOxygene = LibAllocPooled(chipPool, OxygeneSize) ;
  112.             if (!chipOxygene) {
  113.                 LibDeletePool(anyPool) ;
  114.                 LibDeletePool(chipPool) ;
  115.                 anyPool = NULL ;
  116.                 chipPool = NULL ;
  117.                 FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  118.                 MemorySemaphore = NULL ;
  119.             }
  120.         }
  121.         else {
  122.             FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  123.             MemorySemaphore = NULL ;
  124.         }
  125.     }
  126. bye:
  127. #if defined( __GNUG__ )
  128.     if (!MemorySemaphore) exit(20) ;
  129. #endif
  130.     return (MemorySemaphore)?0:1 ;
  131. }
  132.  
  133.  
  134. void _STD_50_freepool()
  135. {
  136.     FreeMem(MemorySemaphore, sizeof(SignalSemaphore)) ;
  137.     if (anyPool) LibDeletePool(anyPool) ;
  138.     if (chipPool) LibDeletePool(chipPool) ;
  139. }
  140. }
  141.  
  142. #if defined( __GNUG__ )
  143. ADD2INIT(_STI_50_initpool, -50);
  144. ADD2EXIT(_STD_50_freepool, -50);
  145. #endif
  146.